﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

using Godot.core.os;

namespace Godot.platform.stub
{
    public class OS_Stub : OS
    {
        public OS_Stub()
        : base()
        {
        }

        public override void initialize_core()
        {
            FileAccess.make_default<FileAccessStub>(FileAccess.AccessType.ACCESS_RESOURCES);
	        FileAccess.make_default<FileAccessStub>(FileAccess.AccessType.ACCESS_USERDATA);
	        FileAccess.make_default<FileAccessStub>(FileAccess.AccessType.ACCESS_FILESYSTEM);
	        //FileAccessBufferedFA<FileAccessUnix>::make_default();
	        DirAccess.make_default<DirAccessStub>(DirAccess.AccessType.ACCESS_RESOURCES);
	        DirAccess.make_default<DirAccessStub>(DirAccess.AccessType.ACCESS_USERDATA);
	        DirAccess.make_default<DirAccessStub>(DirAccess.AccessType.ACCESS_FILESYSTEM);
        }

        public override VideoMode get_default_video_mode()
        {
            return new OS.VideoMode();
        }

        public override void alert(string p_alert, string p_title = "ALERT!")
        {
            
        }

        public override int get_video_driver_count()
        {
            return 2;
        }

        public override int get_audio_driver_count()
        {
            return 0;
        }

        public override string get_audio_driver_name(int p_driver)
        {
            return "OpenAL";
        }

        public override string get_video_driver_name(int p_driver)
        {
            return p_driver == 0 ? "OpenGL" : "DirectX";
        }

        public override void set_main_loop(MainLoop p_main_loop)
        {
            __main_loop = p_main_loop;
        }

        public override void initialize(OS.VideoMode p_desired, int p_video_driver, int p_audio_driver)
        {            
            __default_videomode = new OS.VideoMode();
            __default_videomode.width = 800;
            __default_videomode.height = 600;
        }

        public override string get_name()
        {
            return "Windows";
        }

        public void main_loop_begin() 
        {
	        if (__main_loop != null)
		        __main_loop.init();
        }

        public bool main_loop_iterate() 
        {
	        if (__main_loop == null)
		        return false;

            if (__watch == null)
                __watch = Stopwatch.StartNew();

	        return Godot.main.Main.iteration();
        }

        public void main_loop_end() 
        {
	        if (__main_loop != null)
		        __main_loop.finish();
        }

        public override OS.VideoMode get_video_mode(int p_screen)
        {
            return __default_videomode;
        }

        public override void set_window_title(string appname)
        {
        }

        public override ulong get_ticks_usec()
        {
            ulong time = (ulong)(__watch.Elapsed.TotalMilliseconds * 1000L);
            time -= __tick_start;
            return time;
        }

        public override MainLoop get_main_loop()
        {
            return __main_loop;
        }

        private MainLoop __main_loop;
        private OS.VideoMode __default_videomode;
        private Stopwatch __watch;
        private ulong __tick_start;
    }
}
